package listtest;

import java.util.Arrays;

public class MyArrayList implements  Ilist{
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY =10;
    public MyArrayList(){
        this.array =new int[DEFAULT_CAPACITY];
    }
    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.usedSize]=data;
        usedSize++;
    }
    public boolean isFull(){
        return this.usedSize ==array.length;
    }
    private  void grow(){
        this.array= Arrays.copyOf(this.array,2*array.length);
    }
    public void checkPos(int pos)throws  PosIllegal{
        if(pos<0||pos>usedSize){
            throw new PosIllegal("Pos位置不合法！！");
        }
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
            if(isFull()){
                grow();
            }
            //挪动元素
            for (int i = usedSize-1;i>=pos; i--) {
                array[i+1]=array[i];
            }
            array[pos] =data;
            usedSize++;
        }catch (PosIllegal e){
            System.out.println("插入元素pos位置不合法！！");
            e.printStackTrace();
        }

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(array[i]==toFind){
                return  true;
            }

        }
        return false;
    }
    /*
    1.包含 toFind 返回下标，
    2.否则 返回-1

     */

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(array[i]==toFind){
                return  i;
            }

        }
        return 0;
    }
    public void checkPos2(int pos)throws  PosIllegal{
        if(pos<0||pos>=usedSize){
            throw new PosIllegal("Pos位置不合法！！");
        }
    }
    @Override
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos2(pos);
            return  array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }
    private void checkEmpty(){
        if(isEmpty()){
            throw new EmptyException("顺序表为空!!!");
        }
    }
    public boolean isEmpty(){
        return this.usedSize==0;
    }
    //更新的意思
    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos] =value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos = indexOf(toRemove);
            if (pos == -1) {
                return;
            }
            for (int i = 0; i < usedSize - 1; i++) {
                array[i] = array[i + 1];
            }
            usedSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        /*for (int i = 0; i < usedSize; i++){
            array[i]= Integer.parseInt(null);
        }*/
        usedSize=0;
        return 0;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
